package com.cwh.taskcenter.task.gateway;

import com.alibaba.cola.dto.PageResponse;
import com.cwh.taskcenter.domain.task.model.Task;
import com.cwh.taskcenter.domain.task.gateway.TaskGateWay;
import com.cwh.taskcenter.domain.task.model.TaskType;
import com.cwh.taskcenter.domain.task.model.UserTask;
import com.cwh.taskcenter.domain.task.model.UserTaskStatus;
import com.cwh.taskcenter.task.converter.TaskConverter;
import com.cwh.taskcenter.task.converter.UserTaskConverter;
import com.cwh.taskcenter.task.dto.query.TaskListByEventTypeQry;
import com.cwh.taskcenter.task.dto.query.TaskListByPageQry;
import com.cwh.taskcenter.task.dto.query.TaskListByUserIdQry;
import com.cwh.taskcenter.task.mapper.UserTaskMapper;
import com.cwh.taskcenter.task.model.TaskDO;
import com.cwh.taskcenter.task.mapper.TaskMapper;
import com.cwh.taskcenter.task.model.UserTaskDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 任务网关实现类
 * @author cwh
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class TaskGateWayImpl implements TaskGateWay {
    private final TaskMapper taskMapper;
    private final UserTaskMapper userTaskMapper;

    @Override
    public Long save(Task task) {
        TaskDO taskDO = TaskConverter.toTaskDO(task);
        taskMapper.create(taskDO);
        return taskDO.getId();
    }

    @Override
    public List<Task> getTaskByEventType(TaskListByEventTypeQry req) {
        List<TaskDO> taskDOList = taskMapper.findNormalTaskByEventType(req);
        if (CollectionUtils.isEmpty(taskDOList)) {
            return new ArrayList<>();
        }

        List<Long> taskIds = taskDOList.stream().map(TaskDO::getId).collect(Collectors.toList());
        TaskListByUserIdQry taskListByUserIdQry = new TaskListByUserIdQry();
        taskListByUserIdQry.setUserId(req.getUserId());
        taskListByUserIdQry.setTaskIds(taskIds);
        // 获取最新的一条用户记录
        List<UserTaskDO> userTaskDOList = userTaskMapper.findByUserId(taskListByUserIdQry);
        Map<Long, UserTaskDO> userTaskMap = userTaskDOList.stream()
                .collect(Collectors.toMap(
                        UserTaskDO::getTaskId,
                        Function.identity(),
                        (existing, replacement) ->
                                existing.getCreatedAt().isAfter(replacement.getCreatedAt())
                                        ? existing
                                        : replacement
                ));        return taskDOList.stream().map(taskDO -> {
            Task task = TaskConverter.toTask(taskDO);
            UserTaskDO userTaskDO = userTaskMap.get(taskDO.getId());
            task.setLastUserTask(UserTaskConverter.toUserTask(userTaskDO));
            return task;
        }).collect(Collectors.toList());
    }

    @Override
    public PageResponse<Task> page(TaskListByPageQry req) {
        PageHelper.startPage(req.getPageIndex(), req.getPageSize());
        List<TaskDO> taskDOList = taskMapper.paging(req);
        PageInfo<TaskDO> pageInfo = new PageInfo<>(taskDOList);
        List<Task> taskList = taskDOList.stream().map(TaskConverter::toTask).collect(Collectors.toList());
        return PageResponse.of(taskList, (int) pageInfo.getTotal(), req.getPageSize(), req.getPageIndex());
    }

    @Override
    public Task getById(Long taskId) {
        TaskDO taskDO = taskMapper.findById(taskId);
        return TaskConverter.toTask(taskDO);
    }

}
